package com.zahb.qadx.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.CommonResponse;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.util.ActivityManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by huwei.
 * Data 2020-10-13.
 * Time 09:53.
 * 账户安全
 */
public class AccountSecurityActivity extends BaseActivity {
    @BindView(R.id.rl_update_pwd)
    RelativeLayout rlUpdatePwd;
    @BindView(R.id.tv_exit)
    AppCompatButton tvExit;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected int getLayout() {
        return R.layout.activtiy_account_security;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        tvPhone.setText(getIntent().getStringExtra("phone"));
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.account_security;
    }

    @OnClick({R.id.rl_update_pwd, R.id.tv_exit, R.id.rl_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_update_pwd:
                startActivity(new Intent(AccountSecurityActivity.this, UpdatePwdActivity.class));
                break;
            case R.id.tv_exit:
                exitLogin(null);
                //exit();
                break;
            case R.id.rl_phone:
                Intent intent = new Intent(AccountSecurityActivity.this,ReplacePhone.class);
                intent.putExtra("phone",getIntent().getStringExtra("phone"));
                startActivity(intent);
                break;
        }
    }

    private void exit() {
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .getLoginOut()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        exitLogin(response);
                    }, throwable -> {
                        throwable.printStackTrace();
                        hideProgressDialog();
                    }));
        }
    }

    private void exitLogin(CommonResponse<Object> response) {
        /*if (response.getStatusCode() == 200) {*/
            BaseApplication.getContext().setDataLogin(null);
            showToast(R.string.logout_success);
            SharedPreferences sp = getSharedPreferences("AccountInfo", MODE_PRIVATE);
            sp.edit().putString("loginData", "").apply();
            String[] array = LoginActivity.getAccountInfo(getContext());
            ActivityManager.getInstance().finishActivities(ActivityManager.KEY_ALL_ACTIVITY);
            LoginActivity.actionStart(getContext(), array[0], array[1]);
        /*} else {
            BToast.showText(AccountSecurityActivity.this, response.getErrorInfo());
        }*/
    }
}
